package com.gitee.clarence.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.gitee.clarence.interceptor.log.LogInterceptor;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

	static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };

	public void addInterceptors(InterceptorRegistry registry) {

		registry.addInterceptor(new LogInterceptor())//
				.addPathPatterns("/api/v1/**", "/api/v2/**")//
				.excludePathPatterns("/webjars/**", "/swagger-resources/**", "/static/**", "/swagger.json");
	}

	// 解决跨域问题
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedOrigins("*").allowedMethods(ORIGINS);
	}

}
